package br3akr.DeadlyDinnerParty.Server.Actions;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;

import br3akr.DeadlyDinnerParty.Server.Engine.Session;
import br3akr.DeadlyDinnerParty.Server.Exceptions.DeadlyDinnerPartyException;
import br3akr.DeadlyDinnerParty.Server.World.Area;
import br3akr.DeadlyDinnerParty.Server.World.GameCharacter;

public class GagAction extends Action {
	boolean actionComplete;
    private ActionResponse actionResponse;
    public static final Set<String> keywords;
	static
	{
		Set<String>tempKeywords = new HashSet<String>();
		tempKeywords.add("Gag");
		tempKeywords.add("Ungag");
		tempKeywords.add("Ug");
		keywords = Collections.unmodifiableSet(tempKeywords);
	}
    
    public GagAction() {
        super("Gag", false);
    }
    
    public ActionResponse doAction(String action, Session session) throws DeadlyDinnerPartyException {
		if (action == null) {
			throw new DeadlyDinnerPartyException ("No action found.");
		}
		if (session == null) {
			throw new DeadlyDinnerPartyException ("No session found.");
		}
		
    	StringTokenizer tokenizedAction = new StringTokenizer(action);
    	String keyword = tokenizedAction.nextToken();
    	String param1 = null;
    	
    	if (tokenizedAction.hasMoreTokens()) {
    		param1 = tokenizedAction.nextToken();
    	}
    	
    	if (param1 == null) {
    		throw new DeadlyDinnerPartyException("Who do you want to gag?");
    	}
    	
		GameCharacter actor = session.getCharacter();
		
		if (actor == null) {
			throw new DeadlyDinnerPartyException ("No character found for your session.");
		}
    	
    	actionResponse = new ActionResponse();
    	actionResponse.setActingCharacter(session.getCharacter());
    	Area location = session.getCharacter().getLocation();
    	
    	GameCharacter target = location.getCharacter(param1);
    	
    	if (target == null || target.isHiddenFrom(actor)) {
    		throw new DeadlyDinnerPartyException ("There is no one here by the name of " + param1);
    	}
    	
    	Set<GameCharacter> localOnlookers = location.getAreaOnlookers(session.getCharacter(), null);
    	actionResponse.addAffectedArea(location);
    	
		switch (keyword.toLowerCase()) {
			case "gag":
				if (actor.isRecovering()) {
					throw new DeadlyDinnerPartyException ("You can not gag while recovering.");
				}
				
				if (actor.getHandcuffs() != null) {
					throw new DeadlyDinnerPartyException ("You can not gag while handcuffed.");
				}
				
				if (target.isGagged()) {
					throw new DeadlyDinnerPartyException ("That character is already gagged.");
				}
				
				if (target.getStatus() != GameCharacter.Status.INCAPACITATED && target.getHandcuffs() == null) {
					throw new DeadlyDinnerPartyException ("A character must be incapacitated, or handcuffed in order to be gagged.");
				}
				
				target.setGagged(true);
				actionResponse.addOnlookers(localOnlookers, actor.getName() + " stuffs a wad of cotton in " + target.getName() + "'s mouth and ties it in place.");
				actionResponse.setActingCharacterDescription("You stuff a wad of cotton in " + target.getName() + "'s mouth and tie it in place.");
				actionResponse.addTargetCharacter(target);
				actionResponse.setTargetCharacterDescription(target, actor.getName() + " stuffs a wad of cotton in your mouth, and ties it in place.");
				beginRecovery(actor, 3000);
				break;
			case "ungag":
			case "ug":
				if (actor.isRecovering()) {
					throw new DeadlyDinnerPartyException ("You can not ungag while recovering.");
				}
				
				if (actor.getHandcuffs() != null) {
					throw new DeadlyDinnerPartyException ("You can not ungag while handcuffed.");
				}
				
				if (!target.isGagged()) {
					throw new DeadlyDinnerPartyException ("That character is not gagged.");
				}

				actionResponse.addOnlookers(localOnlookers, actor.getName() + " ungags " + target.getName() + ".");
				actionResponse.setActingCharacterDescription("You ungag " + target.getName() + ".");
				actionResponse.addTargetCharacter(target);
				actionResponse.setTargetCharacterDescription(target, actor.getName() + " removes the gag in your mouth.");
				target.setGagged(false);
				beginRecovery(session.getCharacter(), 3000);
				break;
			default:
				break;
		}
        
    	actionComplete = true;
        return actionResponse;
    }
	
    public ActionResponse getActionResponse() {
		return actionResponse;
	}
    
    public Set<String> getKeywords() {
        return keywords;
    }
}